package org.black.missedCall2;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MissedCallFontSizeDialogFragment extends DialogFragment implements
		OnSeekBarChangeListener {
	public static final int SEEK_BAR_MAX_VALUE = 100;
	public static final int DEFAULT_SEEK_BAR_VALUE = 25;

	public static final String TAG = "MissedCallFontSizeDialogFragmentTag";

	public interface MissedCallFontSizeDialogListener {
		public void onStopSeeking(int seekBarPosition);
	}

	private MissedCallFontSizeDialogListener missedCallFontSizeDialogListener;

	private SeekBar fontSizeSeekBar;

	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(R.layout.missed_call_font_size_dialog,
				null);

		builder.setView(view);
		fontSizeSeekBar = (SeekBar) view
				.findViewById(R.id.missedCallFontSizeDialogSeekBar);
		fontSizeSeekBar.setMax(SEEK_BAR_MAX_VALUE);

		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(getActivity()
						.getApplicationContext());
		int fontSizeScaleValue = sharedPreferences
				.getInt(getString(R.string.seek_bar_position_for_font_size_preference_key),
						DEFAULT_SEEK_BAR_VALUE);
		fontSizeSeekBar.setProgress(fontSizeScaleValue);
		fontSizeSeekBar.setOnSeekBarChangeListener(this);

		builder.setTitle(R.string.font_size).setPositiveButton(R.string.done,
				null);

		return builder.create();
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(getActivity()
						.getApplicationContext());
		Editor editor = sharedPreferences.edit();
		editor.putInt(
				getString(R.string.seek_bar_position_for_font_size_preference_key),
				seekBar.getProgress());
		editor.commit();
		missedCallFontSizeDialogListener.onStopSeeking(seekBar.getProgress());
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			missedCallFontSizeDialogListener = (MissedCallFontSizeDialogListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement MissedCallFontSizeDialogListener");
		}
	}
}
